﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using _3DExample;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Design;
using System.IO;
using Microsoft.Xna.Framework;

namespace PhotoScreenSaver
{
    class screensaverLogic : GameLogic
    {
        public override void Load(Microsoft.Xna.Framework.Content.ContentManager Content)
        {
            
            content = Content;
            disp = content.Load<Model>("slidedisp");
        }
        GraphicsDevice graphicsDevice;
        ContentManager content;
        bool first = true;
        Model disp;
        void populateImages(float x, float y, float z,float scale)
        {
            if (models.Count <= 15)
            {
                try
                {
                    Random mrand = new Random();
                    string[] list = Directory.EnumerateFiles(Program.Path, "*.jpg", SearchOption.AllDirectories).ToArray<string>();
                    string randpath = list[mrand.Next(0, list.Length - 1)];
                    Stream fsStream = File.Open(randpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    
                    Texture2D mytext = Texture2D.FromStream(graphicsDevice, fsStream);
                    fsStream.Close();
                    drawableModel renderModel = new drawableModel(graphicsDevice, disp, mytext);
                    renderModel.position.Y = y;
                    renderModel.position.Z = z;
                    renderModel.position.X = x;
                    renderModel.scale(-900 + scale);
                    renderModel.position.Y += 200;
                    renderModel.onRender += new drawableModel.RenderEventArgs(renderModel_onRender);
                    models.Add(renderModel);
                }
                catch (Exception)
                {
                    populateImages(x, y, z, scale);
                }
            }
        }
        int xc;
        int zc;
        float posx = Camera.Position.X + 500;
        float posy = Camera.Position.Y;
        float posz = Camera.Position.Z;
        float scale = 0;
        float cpz = 0;
        void asyncPopulateImages(float x, float y, float z,float scale)
        {
            System.Threading.Thread mthread = new System.Threading.Thread(asyncCallback);
            mthread.Start(new float[] { x, y, z,scale });
        }
        void asyncCallback(object sender)
        {
            float[] myray = (float[])sender;
            populateImages(myray[0], myray[1], myray[2],myray[3]);
        }
        public override void Update(Microsoft.Xna.Framework.GameTime gameTime, GameLogic.drawer Draw, Microsoft.Xna.Framework.Graphics.GraphicsDevice GraphicsDevice, ref Microsoft.Xna.Framework.Vector2 mousePosition, ref Microsoft.Xna.Framework.GraphicsDeviceManager graphics, ref GlobalGridP2P.P2PCommChannel mychannel, ref bool hist, ref GameLogic mylogic)
        {
            
            if (first)
            {
                posx = Camera.Position.X + 500;
                posy = Camera.Position.Y+200;
                posz = Camera.Position.Z;
                xc = 3;
                zc = 2;
                Camera.Rotation.Y += 7.5f;
                first = false;
                graphicsDevice = GraphicsDevice;
                
               
            }
            
            
            if (zc > 0)
            {
                if (xc > 0)
                {
                    asyncPopulateImages(posx, posy, posz,scale);
                    posx -= 500;

                    xc -= 1;
                }
                else
                {
                    xc = 5;
                    posx = Camera.Position.X + 500;
                    posz += 2000;
                    zc -= 1;
                }
            }
            Camera.Position.Z -= 3;
            cpz += 1;
            }
        
        void renderModel_onRender(drawableModel sender)
        {
            sender.rotation += 0.01f;
          //  BoundingFrustum mytum = sender.collisionList[0];
            float data = 0;
            if (sender.collisionList != null)
            {
                data = sender.collisionList[0].Bottom.D;
            }
            if(data<-6) {
                models.Remove(sender);
                sender.Texture.Dispose();
                sender.Texture = null;
               
                
                
                xc = 5;
               
                zc = 2;


            }
        }

        public override void drawSprites(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
        {
           
        }
    }
}
